package com.luxsoft.siipap.em.replica.importadores.dbf;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import com.luxsoft.siipap.replica.reader.DBFMapConverter;

public class DBFSupport {
	
	protected Logger logger=Logger.getLogger(getClass());
	
	public void execute(final String file,final DBFRowHandler handler,boolean excluirEliminados) throws IOException{
		String path = file;
		FileInputStream inputStream = new FileInputStream(path);
        try{
    		DBFMapConverter reader=new DBFMapConverter(inputStream);
            Map<String, Object> rowObjects;
            while ((rowObjects = reader.nextRecord()) != null) {
            	try {            		
            		boolean deleted=(Boolean)rowObjects.get("ELIMINADO");
            		if(deleted && excluirEliminados)
            			continue;
            		else{
            			handler.processRow(rowObjects);
            		}
            			
				}catch (Exception e) {
					logger.error("Error al tratar de procesar registro:\n "+ExceptionUtils.getRootCauseMessage(e)+"\n"
							+rowObjects,e);
					
					continue;
				}
            }
            
    	}finally{
    		inputStream.close();
    	}
	}

}
